package com.ruoyi.cs.event.customerEvent.service;

import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.base.customer.domain.CustomerInfo;
import com.ruoyi.cs.base.customer.mapper.CustomerInfoMapper;
import com.ruoyi.cs.base.supplier.domain.SupplierInfo;
import com.ruoyi.cs.base.supplier.mapper.SupplierInfoMapper;
import com.ruoyi.cs.event.customerEvent.domain.CustomerEvent;
import com.ruoyi.cs.event.customerEvent.domain.CustomerEventCond;
import com.ruoyi.cs.event.customerEvent.mapper.CustomerEventMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerEventServiceImpl implements ICustomerEventService{
    @Autowired
    private CustomerEventMapper customerEventMapper;
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private IdUtil idUtil;
    /**
     * 查找客户事件
     * @param cond
     * @return
     */
    @Override
    public List<CustomerEvent> selectCustomerEventList(CustomerEventCond cond) {
        List<CustomerEvent> customerEventList = customerEventMapper.selectCustomerEventList(cond);
        for (CustomerEvent s : customerEventList) {
            if (s.getAssociationType().equals(ERPConstant.CS_CUSTOMER)) {
                // 关联类型为客户
                CustomerInfo customerInfo = customerInfoMapper.selectCustomerInfoById(s.getAssociationId());
                s.setAssociationName(customerInfo.getCustomerInfoName());
            } else {
                // 关联类型为供应商
                SupplierInfo supplierInfo = supplierInfoMapper.selectSupplierInfoById(s.getAssociationId());
                s.setAssociationName(supplierInfo.getSupplierInfoName());
            }
        }
        return customerEventList;
    }

    /**
     * 新增客户事件
     * @param customerEvent
     * @return
     */
    @Override
    public int insertCustomerEvent(CustomerEvent customerEvent) {
        if (StringUtils.isNotNull(customerEventMapper.selectCustomerEventById(customerEvent.getEventId()))) {
            //存在
            return 0;
        }
        //不存在
        String eventId = idUtil.createEventId();
        customerEvent.setEventId(eventId);
        return customerEventMapper.insertCustomerEvent(customerEvent);
    }

    /**
     * 更新客户事件
     * @param customerEvent
     * @return
     */
    @Override
    public int updateCustomerEvent(CustomerEvent customerEvent) {
        if (!StringUtils.isNotNull(customerEventMapper.selectCustomerEventById(customerEvent.getEventId()))) {
            //不存在
            return 0;
        }
        //存在销售订单出库单据
        return customerEventMapper.updateCustomerEvent(customerEvent);
    }

    /**
     * 删除客户事件
     * 仅修改客户事件删除标志
     * @param customerEvent
     * @return
     */
    @Override
    public int updateCustomerEventDeleteFlag(CustomerEvent customerEvent) {
        return customerEventMapper.updateCustomerEventDeleteFlag(customerEvent);
    }
}
